package com.demo.utils;



import java.text.SimpleDateFormat;


import cn.hutool.core.date.ChineseDate;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.github.heqiao2010.lunar.LunarCalendar;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.*;

public class DateUtils {
    private static final Object lockObj = new Object();
    private static Map<String, ThreadLocal<SimpleDateFormat>> sdfMap = new HashMap();


    private static final int[] LUNAR_MONTH_DAYS = {30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30};

    private static SimpleDateFormat getSdf(final String patten) {
        ThreadLocal<SimpleDateFormat> t1 = sdfMap.get("patten");
        if (t1 == null) {
            synchronized (lockObj) {
                t1 = sdfMap.get(patten);
                if (t1 == null) {
                    t1 = new ThreadLocal<SimpleDateFormat>() {
                        @Override
                        protected SimpleDateFormat initialValue() {
                            return new SimpleDateFormat(patten);
                        }
                    };
                    sdfMap.put(patten, t1);
                }
            }
        }
        return t1.get();
    }

    /**
     * 日期格式化
     *
     * @param date
     * @param patten
     * @return
     */
    public static String format(Date date, String patten) {
        return getSdf(patten).format(date);
    }

    public static Date parse(String dateStr, String patten) {
        try {
            return getSdf(patten).parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date getAfterDay(Integer days) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, days);
        return calendar.getTime();
    }

    /**
     * 根据出生年月日计算年龄
     *
     * @param birthday
     * @module
     * @author SJT
     * @date 2022/8/31
     * @return: int
     */
    public static int getAgeByBirth(Date birthday) {

        //获取当前时间
        Calendar cal = Calendar.getInstance();

        //获取出生日期的Calendar对象
        Calendar bir = Calendar.getInstance();
        bir.setTime(birthday);
        //如果出生日期大于当前日期，则返回0
        if (cal.before(birthday)) {
            return 0;
        }
        //取出当前年月日
        int nowYear = cal.get(Calendar.YEAR);
        int nowMonth = cal.get(Calendar.MONTH);
        int nowDay = cal.get(Calendar.DAY_OF_MONTH);

        //取出出生日期的年月日
        int birthYear = bir.get(Calendar.YEAR);
        int birthMonth = bir.get(Calendar.MONTH);
        int birthDay = bir.get(Calendar.DAY_OF_MONTH);

        //计算年份
        int age = nowYear - birthYear;

        //计算月份和日，看看是否大于当前月日，如果小于则减去一岁
        if (nowMonth < birthMonth || (nowMonth == birthMonth && nowDay < birthDay)) {
            age--;
        }
        return age+1;
    }


    public static String getLunarStr(Date date){
        String format = DateUtil.format(date, "yyyy-MM-dd");
        String[] strings = format.split("-");
        int month=Integer.parseInt(strings[1].startsWith("0")?strings[1].substring(1):strings[1]);
        int day=Integer.parseInt(strings[2].startsWith("0")?strings[2].substring(1):strings[2]);

        // 创建农历日期
        ChineseDate lunarDate = new ChineseDate(Integer.parseInt(strings[0]), month,day);
       return lunarDate.toString();
    }

    public static List<Calendar> getThisYearBirthDayOrNextYear(Date date) {

        String format = DateUtil.format(date, "yyyy-MM-dd");
        String[] strings = format.split("-");
        int month=Integer.parseInt(strings[1].startsWith("0")?strings[1].substring(1):strings[1]);
        int day=Integer.parseInt(strings[2].startsWith("0")?strings[2].substring(1):strings[2]);

        // 创建农历日期
        ChineseDate lunarDate = new ChineseDate(Integer.parseInt(strings[0]), month,day);

        String[] originBirthMonthAndDay = lunarDate.toString().split(" ");

        // 创建Calendar实例表示该年第一天
        Calendar calendar1 = Calendar.getInstance();
        int currentYear = calendar1.get(Calendar.YEAR);

        calendar1.set(currentYear, Calendar.JANUARY, 1); // 设置为1月1日

        Calendar thisYearDate = getTheDay(calendar1, currentYear, originBirthMonthAndDay[1]);

        ArrayList<Calendar> calendarsList = new ArrayList<>();
        calendarsList.add(thisYearDate);

        Calendar calendar2 = Calendar.getInstance();
        int nextYear = calendar2.get(Calendar.YEAR)+1;
        calendar2.set(nextYear, Calendar.JANUARY, 1); // 设置为1月1日
        Calendar nextYearDate = getTheDay(calendar2, nextYear, originBirthMonthAndDay[1]);
        calendarsList.add(nextYearDate);
        return calendarsList;

    }

    private static Calendar getTheDay(Calendar calendar,Integer year,String target){
        while (calendar.get(Calendar.YEAR) == year) {
            LunarCalendar lunar = LunarCalendar.solar2Lunar(calendar);
            String fullLunarName = lunar.getFullLunarName();
            String substring = fullLunarName.substring(5, fullLunarName.indexOf(" "));
            if (substring.equals(target)) {
                break;
            }
            // 增加一天
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        return calendar;
    }


}










